1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import java.io.IOException;
34 import java.security.NoSuchAlgorithmException;
35 import org.ietf.jgss.GSSException;
36 import org.ietf.jgss.Oid;
37 import javax.crypto.EncryptedPrivateKeyInfo;
38 import sun.security.util.*;
39 import java.util.Arrays;
40
41 public class OidFormat {
42 public static void main(String[] args) throws Exception {
43 String[] badOids = {
44
45 "0", "1", "2",
46 "3.1.1", "3", "4",
47 "1.40", "1.111.1",
48 "-1.2", "0,-2", "1.-2", "2.-2",
49 "1.2.-3.4", "1.2.3.-4",
50
51 "aa", "aa.aa",
52 "", "....", "1.2..4", "1.2.3.", "1.", "1.2.", "0.1.",
53 "1,2",
54 };
55
56 for (String s: badOids) {
57 testBad(s);
58 }
59
60 String[] goodOids = {
61 "0.0", "0.1", "1.0", "1.2",
62 "0.39", "1.39", "2.47", "2.40.3.6", "2.100.3", "2.123456.3",
63 "1.2.3", "1.2.3445",
64 "1.3.6.1.4.1.42.2.17",
65
66 "2.16.764.1.3101555394.1.0.100.2.1",
67 "2.2726957624935694386592435",
68 "1.2.777777777777777777",
69 "1.2.888888888888888888.111111111111111.2222222222222.33333333333333333.44444444444444",
70 "1.2." +
71 "1111111111111111111111111111111111111111111111111111111111111." +
72 "2222222222222222222222222222222222222222222222222222222222222222." +
73 "333333333333333333333333333333333333333333333333333333333333333." +
74 "4444444444444444444444444444444444444444444444444444444." +
75 "55555555555555555555555555555555555555555555555555555555555555555555555." +
76 "666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666." +
77 "77777777777777777777777777777777777777777777777777777777777777777777777777." +
78 "8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888." +
79 "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
80 "1.2.2147483647.4",
81 "1.2.268435456.4",
82 };
83
84 for (String s: goodOids) {
85 testGood(s);
86 }
87
88 int[][] goodInts = {
89 {0,0}, {0,1}, {1,0}, {1,2},
90 {0,39}, {1,39}, {2,47}, {2,40,3,6}, {2,100,3}, {2,123456,3},
91 {1,2,3}, {1,2,3445},
92 {1,3,6,1,4,1,42,2,17},
93 };
94
95 for (int[] is: goodInts) {
96 testGood(is);
97 }
98
99 int[][] badInts = new int[][] {
100 {0}, {1}, {2},
101 {3,1,1}, {3}, {4},
102 {1,40}, {1,111,1},
103 {-1,2}, {0,-2}, {1,-2}, {2,-2},
104 {1,2,-3,4}, {1,2,3,-4},
105 };
106
107 for (int[] is: badInts) {
108 testBad(is);
109 }
110
111 }
112
113 static void testBad(int[] ints) throws Exception {
114 System.err.println("Trying " + Arrays.toString(ints));
115 try {
116 new ObjectIdentifier(ints);
117 throw new Exception("should be invalid ObjectIdentifier");
118 } catch (IOException ioe) {
119 System.err.println(ioe);
120 }
121 }
122
123 static void testGood(int[] ints) throws Exception {
124 System.err.println("Trying " + Arrays.toString(ints));
125 ObjectIdentifier oid = new ObjectIdentifier(ints);
126 DerOutputStream os = new DerOutputStream();
127 os.putOID(oid);
128 DerInputStream is = new DerInputStream(os.toByteArray());
129 ObjectIdentifier oid2 = is.getOID();
130 if (!oid.equals((Object)oid2)) {
131 throw new Exception("Test DER I/O fails: " + oid + " and " + oid2);
132 }
133 }
134
135 static void testGood(String s) throws Exception {
136 System.err.println("Trying " + s);
137 ObjectIdentifier oid = new ObjectIdentifier(s);
138 if (!oid.toString().equals(s)) {
139 throw new Exception("equal test fail");
140 }
141 DerOutputStream os = new DerOutputStream();
142 os.putOID(oid);
143 DerInputStream is = new DerInputStream(os.toByteArray());
144 ObjectIdentifier oid2 = is.getOID();
145 if (!oid.equals((Object)oid2)) {
146 throw new Exception("Test DER I/O fails: " + oid + " and " + oid2);
147 }
148 }
149
150 static void testBad(String s) throws Exception {
151 System.err.println("Trying " + s);
152 try {
153 new ObjectIdentifier(s);
154 throw new Exception("should be invalid ObjectIdentifier");
155 } catch (IOException ioe) {
156 System.err.println(ioe);
157 }
158
159 try {
160 new Oid(s);
161 throw new Exception("should be invalid Oid");
162 } catch (GSSException gsse) {
163 ;
164 }
165
166 try {
167 new EncryptedPrivateKeyInfo(s, new byte[8]);
168 throw new Exception("should be invalid algorithm");
169 } catch (NoSuchAlgorithmException e) {
170 ;
171 }
172 }
173 }